/*
 * Copyright (c) 2023, Cshoo affiliates. All rights reserved.
 */

package org.cshoo.sf.price;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.cshoo.sf.entity.PurchaseItem;

import java.math.BigDecimal;
import java.util.List;
import java.util.Properties;

/**
 * @author 杨翼
 * @version 1.0
 * @since 1.0
 */
@Data
@Slf4j
public class GeneralStrategy implements PriceStrategy {

    private Properties properties;

    public BigDecimal gainUnitPrice(String fruit) {
        String property = properties.getProperty(fruit);
        return new BigDecimal(property);
    }

    public BigDecimal calcTotalPrice(List<PurchaseItem> purchaseItems) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (PurchaseItem purchaseItem : purchaseItems) {
            log.info("purchaseItem:{}", purchaseItem);
            BigDecimal unitPrice = this.gainUnitPrice(purchaseItem.getFruit());
            BigDecimal curFruitPrice = unitPrice.multiply(new BigDecimal(purchaseItem.getWeight()));
            totalPrice = totalPrice.add(curFruitPrice);
        }
        return totalPrice;
    }

}
